package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.domain.*;
import com.uwlaser.service.AntonConfigDeviceService;
import com.uwlaser.service.AntonConfigService;
import com.uwlaser.service.FactoryModelService;
import com.uwlaser.service.dto.AntonConfigDto;
import com.uwlaser.service.dto.FactoryAttributeDto;
import com.uwlaser.service.dto.FactoryModelDto;
import com.uwlaser.service.mapper.AntonConfigDeviceMapper;
import com.uwlaser.service.mapper.AntonConfigMapper;
import com.uwlaser.service.mapper.FactoryModelMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import lombok.AllArgsConstructor;
import org.redisson.misc.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yangwei
 * @create 2022/10/13
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AntonConfigServiceImpl extends CommonServiceImpl<AntonConfigMapper, AntonConfig> implements AntonConfigService{

    @Autowired
    private final AntonConfigMapper antonConfigMapper;

    /**
     * 安东配置与设备关联查询mapper
     */
    @Autowired
    private final AntonConfigDeviceMapper antonConfigDeviceMapper;

    @Autowired
    private final AntonConfigDeviceService antonConfigDeviceService;

    @Override
    public PageInfo<AntonConfigDto> queryAll(AntonConfigParam query, Pageable pageable) {
        IPage<AntonConfig> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<AntonConfig> page = antonConfigMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        PageInfo<AntonConfigDto> antonConfigDtoPageInfo = ConvertUtil.convertPage(page, AntonConfigDto.class);
        List<AntonConfigDto> content = antonConfigDtoPageInfo.getContent();
        for (AntonConfigDto antonConfigDto : content) {
            antonConfigDto.setAntonConfigDevices(antonConfigDeviceMapper.getAntonDeviceByAntonCode(antonConfigDto.getAntonCode()));
        }
        antonConfigDtoPageInfo.setContent(content);
        return antonConfigDtoPageInfo;
    }

    @Override
    public AntonConfig getById(Integer id) {
        return antonConfigMapper.selectById(id);
    }

    @Override
    public AntonConfigDto findById(Integer id) {
        AntonConfigDto convert = ConvertUtil.convert(getById(id), AntonConfigDto.class);
        convert.setAntonConfigDevices(antonConfigDeviceMapper.getAntonDeviceByAntonCode(convert.getAntonCode()));
        return convert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(AntonConfigDto resources) {
        if(CollectionUtils.isEmpty(resources.getAntonConfigDevices())){
            throw new RuntimeException("请添加设备关联关系!");
        }
        AntonConfig entity = ConvertUtil.convert(resources, AntonConfig.class);
         AntonConfig antonConfig = new AntonConfig();
        antonConfig.setAntonCode(entity.getAntonCode());
        Integer count = antonConfigMapper.selectCount(new QueryWrapper<>(antonConfig));
        if(count >= 1){
            throw new RuntimeException("安东编码【"+entity.getAntonCode()+"】不能重复！");
        }

        for (AntonConfigDevice antonConfigDevice : resources.getAntonConfigDevices()) {
            AntonConfigDevice device = new AntonConfigDevice();
            device.setAntonCode(resources.getAntonCode());
            device.setDeviceCode(antonConfigDevice.getDeviceCode());
            Integer deviceCount = antonConfigDeviceMapper.selectCount(new QueryWrapper<>(device));
            if(deviceCount>0){
                throw new RuntimeException("设备【"+antonConfigDevice.getDeviceName()+"/"+antonConfigDevice.getDeviceCode()+"】已经存在，不能重复添加！");
            }
            antonConfigDeviceMapper.insert(antonConfigDevice);
        }
        return antonConfigMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateToId(AntonConfigDto resources) {
        AntonConfig entity = ConvertUtil.convert(resources, AntonConfig.class);
        int res = antonConfigMapper.updateById(entity);
        AntonConfig antonConfig = new AntonConfig();
        antonConfig.setAntonCode(entity.getAntonCode());
        Integer count = antonConfigMapper.selectCount(new QueryWrapper<>(antonConfig));
        if(count >= 2){
            throw new RuntimeException("安东编码【"+entity.getAntonCode()+"】不能重复！");
        }
        //先删除原来数据
        AntonConfigDevice antonConfigDeviceTemp = new AntonConfigDevice();
        antonConfigDeviceTemp.setAntonCode(resources.getAntonCode());
        antonConfigDeviceMapper.delete(new QueryWrapper<>(antonConfigDeviceTemp));

        for (AntonConfigDevice antonConfigDevice : resources.getAntonConfigDevices()) {
            AntonConfigDevice device = new AntonConfigDevice();
            device.setAntonCode(resources.getAntonCode());
            device.setDeviceCode(antonConfigDevice.getDeviceCode());
            Integer deviceCount = antonConfigDeviceMapper.selectCount(new QueryWrapper<>(device));
            if(deviceCount>0){
                throw new RuntimeException("设备【"+antonConfigDevice.getDeviceName()+"/"+antonConfigDevice.getDeviceCode()+"】已经存在，不能重复添加！");
            }
        }
        //修改关联表
        antonConfigDeviceService.saveBatch(resources.getAntonConfigDevices());
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id) {
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            AntonConfigDto antonConfigDto = findById(id);
            Set<Integer> deviceIds = new HashSet<>();
            for (AntonConfigDevice antonConfigDevice : antonConfigDto.getAntonConfigDevices()) {
                deviceIds.add(antonConfigDevice.getId());
            }
            antonConfigDeviceService.removeByIds(deviceIds);
        }
        return antonConfigMapper.deleteBatchIds(ids);
    }


}
